#ifndef DATAPROVIDER_H
#define DATAPROVIDER_H


#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <QDebug>
#include <QDateTime>
#include <QObject>
#include <QThread>
#include <QKeyEvent>
#include <QStringList>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QSettings>
#include <QImage>
#include <QDir>
#include "macro_def.h"

typedef struct{
    unsigned int  reg_num;
    unsigned int  reg_setval;
    unsigned int  readsize;
    unsigned int* readdata;
} s_FusionReg;

#define FUSION_MAGIC  'k'
#define GET_FUSION_REGS _IOR(FUSION_MAGIC,  1,int)
#define SET_FUSION_REGS _IOW(FUSION_MAGIC,  1,s_FusionReg)
#define GET_FUSION_MEMS _IOR(FUSION_MAGIC,  1,s_FusionReg)

//熔接状态 为了方便 注意顺序
typedef enum
{
    Idle,               //0
    SysCheck,
    TestSave,
    MeltReset,
    MeltInit,
    MeltQuickPush,
    MeltClean,
    MeltRoughAlign,
    MeltSlowPush,
    MeltPreciseAlign,
    MeltPause,          //10
    MeltAlignDetial,
    MeltProc,
    MeltDone,
    MeltAgain,
    MeltTention,
    MeltDebug,
    MeltPwrSave,
    MeltPwrDown,
    MeltBaseErr,
    MeltCamErr,           //20
    MeltFiberError,
    MeltRecalib
}MeltState;

typedef enum
{
    Auto,
    Manual,
    Pause1,
    Pause2,
    Pause3,
    Calibe
}MeltMode;

typedef enum
{
    AutoHeating,
    ManualHeating
} HeatingMode;

typedef enum
{
    Fast,
    Precise
}SpeedMode;

typedef enum
{
    ElastTestOn,
    ElastTestOff
}ElastTest;

typedef enum
{
    LightOn,
    LightOff
}Light;

typedef enum
{
    Chn,
    Eng
}Language;

class DataProvider:public QThread
{

    Q_OBJECT

public:
    DataProvider();
    void setLanguageTr(QStringList strTrList);

private:
    MeltState meltState;
    MeltMode meltMode;
    HeatingMode heatingMode;
    SpeedMode spdMode;

    ElastTest elastTest;
    Light light;
    Language language;

    bool dirct_x;
    bool dirct_y;
    int  dirct_change_x;
    int  dirct_change_y;
    int  align_cnt_x;
    int  align_cnt_y;
    int  burst_points;
    int  max_allow_change;
    int  fusion_max_align;
    int  edge_rough_offset;
    int  face_result_xl;
    int  face_result_xr;
    int  face_result_yl;
    int  face_result_yr;

    int  standby_t;
    int  pwroff_t;
    int  fd;
    int  FiberGap;
    int  FiberDiameterMaxDiff;
    int  FiberMaxAngle;
    int  FiberRoughPoints;
    int  FiberAccuratePoints;
    int  FiberBlcakLevel;
    int  fusion_max_diam_diff;
    int  fusion_max_core_diff;

    int  gap_info[4];
    int  joint_info[30];

    int  battery_incharge;
    int  battery_grd_accum;
    int  battery_cnt_accum;
    int  battery_grad;
    int  estimatedLoss;
    int  nProgParamSel;

    qreal cover;
    qreal fiberCore;
    qreal fiberAngle;
    qreal cutAngleLeft;
    qreal cutAngleRight;

    int PositionSuggested;//建议放电位置
    int IntenseSuggested;//建议放电强度
    int PreIntenseSuggested;//建议预放电强度
    QStringList records;
    QSqlDatabase db;

    unsigned int ov_id;
    unsigned int bob_allow;
    unsigned int print_level;

    unsigned int fiberx_info[256];
    unsigned int fibery_info[256];
    unsigned int fiberx_edge[256];
    unsigned int fibery_edge[256];
    unsigned int fiberx_core[256];
    unsigned int fibery_core[256];
    QSettings* settings;

    QString melt_Debug;
    QString melt_PwrDown;
    QString melt_Init;
    QString melt_QuickPush;
    QString melt_SlowPush;
    QString melt_PreciseAlign;
    QString melt_Pause;
    QString melt_Proc;
    QString melt_Save;
    QString melt_estimatedLoss;
    QString melt_failure;
    QString melt_Tention;
    QString melt_Report;
    QString melt_Error;
    QString melt_free;

    QString mode_Auto;
    QString mode_Manual;
    QString mode_Pause1;
    QString mode_Pause2;
    QString mode_Pause3;
    QString mode_Calibe;

    QString spd_fast;
    QString spd_precise;

    QString Heating_Auto;
    QString Heating_Manual;

    float my_batPer;
    int my_elecConut;
    int bat_adc_nm;
    QStringList my_adminPress;
    bool my_cmosStartOver;
    int my_cmosMessageEn;
    int my_cmosCount;
    int my_poweroffCount;
    bool my_messageEnable;

    int my_lastBarNumber;
public:
    //获取所有当前系统设置值
    void InitBatGrade();
    void clearFusionCfgs();
    void InitFusionCfgs();
    void GetJointInfo(int info[30]);
    void setRetValue(QString name);
    //actions
    void ManulFandian(unsigned int TimeVal,unsigned int ForceVal);
    //系统时间
    QDateTime getCurrentTime();
    void setCurrentTime(QDateTime);
    QList<int>  getBatGrade();
    int  getUnlockKey();
    float getBattery();


    char rtc_read_byte(unsigned int addr);
    void rtc_write_byte(unsigned int addr, unsigned int data);

    void InitSystemTime();
    void set_rtc_remote_time(unsigned int Timestamp[6]);
    void get_rtc_remote_time(unsigned int Timestamp[6]);

    //V型照明槽
    bool getVLight();
    void setVLight(bool);
    //音量设置
    int getVolume();
    void setVolume(int);
    void BeepTone(int tone[5],int step);
    //语言设置
    int getLanguage();
    void setLangage(int);

    void Fiber_edge_x(unsigned int* m3,unsigned int* m4);
    void Fiber_edge_y(unsigned int* m3,unsigned int* m4);
    void Fiber_bright(unsigned int* m3,unsigned int* m4);

    unsigned int getPrintLevel();
    void IncPrintLevel();
    void DecPrintLevel();
    //熔接状态
    void      setMeltState(MeltState ms);
    MeltState getMeltState();
    QString   getMeltStateStr();
    //速度模式
    SpeedMode getSpeedMode();
    QString getSpeedModeStr();
    void setSpeedMode(SpeedMode spdMode);
    //熔接模式
    void setMeltMode(MeltMode m);
    MeltMode getMeltMode();
    QString getMeltModeStr();

    void UpdateHeatMode(HeatingMode hm);
    void UpdateHeatTime(unsigned int ht);

    HeatingMode getHeatingMode();
    QString getHeatingModeStr();
    void setHeatingMode(HeatingMode hMode);
    //损耗
    int   getEstimatedLoss();
    int   getManualSpeed();
    //熔接记录
    void cleanRecords();
    void setRecords(int startP);
    QStringList getRecords(bool ifSucceeded);
    QList<int> getWeldResult();
    //放电测试
    void  getCaliberationInfo(int info[5]);
    void  cliberate_process();
    qreal getDischargingIntense();
    qreal getDischargingPosition();
    qreal getDischargingIntenseSuggestion();
    qreal getDischargingPositionSuggestion();
    void applyDischargingSuggestion(unsigned int suggest[2]);
    //
    void setDisplayX();
    void setDisplayXY();
    void setDisplayY();

    QStringList getJoint_process();



    void camera0_write_i2c(unsigned short addr,unsigned char data);
    void camera1_write_i2c(unsigned short addr,unsigned char data);

    //void OV5640_read_id(unsigned int  *ov_id);
    //int  OV5640_init_setting(void);
    //void OV5640_reset(void);
    //void OV5640_vga30fps_config(void);
    //void OV5640_capture_start(void);
    //void OV5640_init(void);
    void OV5640_AGC(int agc_on);
    void OV5640_AEC(int aec_on);
    void OV5640_XOFFSET(int camera,int offset);
    void OV5640_YOFFSET(int camera,int offset);

    void setIniItem(char * ItemName ,int ItemVal);
    int  getIniItemVal(char * ItemName);
    bool HaskeyExis(QString key);
    QString getItems(QString ItemName);

    void AdjustNewYPostion();
    void SysCheckProc();
    unsigned int getFaceResult();

    void setNotPush(bool enable);

    void run();
    void TestProg();
    void InitSystem();
    void ExecAlignFiber();
    void ExecAdjustFiber();
    void SetFusionReg(unsigned int reg_num,unsigned int reg_val);
    unsigned int GetFusionReg(unsigned int reg_num);
    void GetFusionMem(unsigned int mem_offset,unsigned int mem_size,unsigned int* mem_buf);

    unsigned int average10(volatile unsigned int source[10]);
    unsigned int duanmian_process(unsigned int q[50], unsigned int d[50],unsigned int c[50],unsigned int *diameter,unsigned int *core,unsigned int *face);

    void BaseLineReset(int *baseline);
    void joint_process_x();
    void joint_process_y();
    void pushing_speed_set(unsigned int speed);
    void adjust_speed_set(unsigned int speed);

    void l_motor_ctrl(unsigned int cpu_ctrl,unsigned int speed,unsigned int dirction,unsigned int on_off,unsigned int pause,unsigned int angle);
    void r_motor_ctrl(unsigned int cpu_ctrl,unsigned int speed,unsigned int dirction,unsigned int on_off,unsigned int pause,unsigned int angle);
    void x_motor_ctrl(unsigned int cpu_ctrl,unsigned int speed,unsigned int dirction,unsigned int on_off,unsigned int pause,unsigned int angle);
    void y_motor_ctrl(unsigned int cpu_ctrl,unsigned int speed,unsigned int dirction,unsigned int on_off,unsigned int pause,unsigned int angle);

    unsigned int x_edge_move(unsigned int m3,unsigned int m4,unsigned int diff_prev,unsigned int* diff_get);
    unsigned int y_edge_move(unsigned int m3,unsigned int m4,unsigned int diff_prev,unsigned int* diff_get);
    unsigned int x_core_move(unsigned int m3,unsigned int m4,unsigned int diff_prev,unsigned int* diff_get);
    unsigned int y_core_move(unsigned int m3,unsigned int m4,unsigned int diff_prev,unsigned int* diff_get);

    void Get_FiberXEdge(unsigned int edge_cnt);
    void Get_FiberYEdge(unsigned int edge_cnt);

    void Get_FiberCoreX(
            unsigned int* core_l,unsigned int* core_r,
            unsigned int* diameter_l, unsigned int *diameter_r,
            unsigned int* face_l, unsigned int *face_r);
    void Get_FiberCoreY(
            unsigned int* core_l,unsigned int* core_r,
            unsigned int* diameter_l, unsigned int *diameter_r,
            unsigned int* face_l, unsigned int *face_r);

    void Fiber_adjust_x();
    void Fiber_adjust_y();
    void Fiber_align_x ();
    void Fiber_align_y ();

    bool  hasFace();

    void UIChange(int id);
    void SetPwrOff(unsigned int t);
    void pwroff();
    void updatePwroff();
    void SetStandBy(unsigned int t);
    void AddDbSpliceRecord();
    int GetPwrOff();
    int GetStandBy();
    void btPressEvent();
    void SetPushKey();

    void screenshotManual();
    void screenshotState(QString stateName, bool saveEnable = false,int modeNum = 0);
    void dataToPic(QString fromData, QString toPic);

    void heatingWhile(bool enable);



signals:   //所有信号都需要申明在signals语句内
    void updateMeltState();
    void updateDynamicUI(int);
    //void displayCMOS();
    //void SigTest(QString text);   //自定义的带参数的信号
    void reboot();
    void powerOff(int);
    void buttonPress();//配置界面有键盘事件响应
    void copyProg(QString fileName);
public slots:
    void FusionActions();
    void copyUDisk();

};

#endif // DATAPROVIDER_H
